package stack;

/**
 * 虚拟机栈
 */
public class TestStackSize {

    private int count;

    /**
     * 测试栈大小
     * 添加运行参数
     * 无参数 - 10778
     * -Xss1M - 18498
     * -Xss2M - 40222
     * -Xss3M - 62072
     */
    public void testStackSize() {
        try {
            recursion();
        } catch(Throwable throwable) {
            System.out.printf("count -> %d\n", count);
            throwable.printStackTrace();
        }
    }

    /**
     * 测试局部变量表
     * 无参数 - 10164
     */
    public void testVar() {
        try {
            testVar1(0, 1, 2);
        } catch (Throwable throwable) {
            System.out.printf("count -> %d\n", count);
            throwable.printStackTrace();
        }
    }

    /**
     * 测试垃圾回收
     * 添加参数：-XX:+PrintGC
     */
    public void testGc() {
        testGc1();
        System.gc();
        System.out.println("gc ok!");

        testGc2();
    }

    private void testGc1() {
        {byte a[] = new byte[1024 * 1024];}
        System.gc();
        System.out.println("gc1 ok!");
    }

    private void testGc2() {
        {byte a[] = new byte[1024 * 1024];}
        int b = 0;
        System.gc();
        System.out.println("gc2 ok!");
    }

    private void testVar1(long a1, long a2, long a3) {
        count ++;
        long d1 = a1, d2 = a2, d3 = a3;
        testVar1(d1, d2, d3);
    }

    private void recursion() {
        count ++;
        recursion();
    }
}
